﻿package com.dykj.platform.iadapter.utils;

import java.text.SimpleDateFormat;
import java.util.*;

import com.dykj.platform.iadapter.common.Constants;

public class ActionUtils {

	private ActionUtils() {
	}

	public static String[] list2Array(List list, String datePattern,
			String recordValueSeparator) {
		String result[] = null;
		Object object[] = null;
		int size = list != null ? list.size() : 0;
		result = new String[size];
		for (int i = 0; i < size; i++) {
			if (list.get(i) instanceof Object[]) {
				object = (Object[]) list.get(i);
			} else if(list.get(i) instanceof Map){
				Map map=(Map)list.get(i);
				object=new Object[map.size()];
				Iterator it=map.entrySet().iterator();
				int j=0;
				while(it.hasNext()){
					Map.Entry entry=(Map.Entry)it.next();
					object[j]=entry.getValue();
					j++;
				}	
			} else {
				object = new Object[1];
				object[0] = list.get(i);
			}
			String str = "";
			for (int j = 0; j < object.length; j++) {
				if (str.length() > 0)
					str = str + recordValueSeparator;
				if (object[j] instanceof Date) {
					SimpleDateFormat sdf = new SimpleDateFormat();
					sdf.applyPattern(datePattern);
					str = str + sdf.format(object[j]);
				} else if (object[j] != null)
					str = str + object[j].toString();
				else if(object[j] ==null)
					str = str + "null";
			}

			result[i] = str;
		}

		return result;
	}

	public static String[] list2Array(List list) {
		return list2Array(list, "yyyyMMddHHmmss");
	}

	public static String[] list2Array(List list, String datePattern) {
		return list2Array(list, datePattern, Constants.RECORD_SEPARATOR);
	}
}
